package com.zuipin.controller;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zuipin.entity.DrawNum;
import com.zuipin.entity.Prize;
import com.zuipin.entity.WinningRecord;
import com.zuipin.framework.annotation.Valid;
import com.zuipin.framework.annotation.Valids;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.exception.CustomException;
import com.zuipin.framework.result.PageResult;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IDrawNumService;
import com.zuipin.service.IWinningRecordService;
import com.zuipin.service.IWinningRuleService;
import com.zuipin.util.RegexUtils;
import com.zuipin.vo.DistributeResultVo;
import com.zuipin.vo.WinningRecordVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 抽奖次数
 *
 * @ClassName DrawNumController
 * @author xujinding
 * @date 2017年5月9日 上午9:50:11
 */

@Controller
@RequestMapping(value = "/drawnum")
@Api(value = "抽奖次数-xujinding", description = "抽奖次数-xujinding")
public class DrawNumController {
	@Resource
	private IWinningRecordService	winningRecordService;
	@Resource
	private IDrawNumService			drawNumService;
	@Resource
	private IWinningRuleService		winningRuleService;
	
	@ResponseBody
	@RequestMapping(value = "/toSendDrawNum", method = RequestMethod.POST)
	@ApiOperation(value = "派发抽奖次数接口-xujinding", notes = "派发抽奖次数接口-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>4020-规则不存在")
	@Valids({ @Valid(name = "isMemberLevel", required = true, minValue = 0, maxValue = 1), @Valid(name = "num", required = true, minValue = 1, maxValue = 999999) })
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysUserId", value = "操作者ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "ruleSetId", value = "抽奖规则ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "isMemberLevel", value = "是否根据会员等级派发", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "num", value = "派发数量", required = true, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "acceptor", value = "会员帐号或等级", required = true, dataType = "long", paramType = "query"), })
	public Result<DistributeResultVo> toSendDrawNum(Long sysUserId, Long sysId, Long ruleSetId, Short isMemberLevel, String acceptor, Integer num)
			throws ParseException, CustomException {
		Result<DistributeResultVo> result = drawNumService.toSendDrawNum(sysUserId, sysId, ruleSetId, isMemberLevel, acceptor, num);
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getDrawResult", method = RequestMethod.POST)
	@ApiOperation(value = "获取抽奖结果-xujinding", notes = "获取抽奖结果-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常" + "</br>4020-查无规则" + "</br>7006-规则未激活"
			+ "</br>7007-不再活动时间里" + "</br>7008-会员等级不符合要求" + "</br>7009-抽奖次数不足" + "</br>70010-平台限制" + "</br>70011-100%中奖默认奖品布不能为空")
	@Valids({ @Valid(name = "device", required = true, regex = "PC|WAP|PC&WAP"), })
	@ApiImplicitParams({ @ApiImplicitParam(name = "serialNumber", value = "规则序列号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "平台", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<Prize> getDrawResult(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam String serialNumber, @RequestParam String device)
			throws ParseException, CustomException {
		Result<Prize> result = winningRuleService.getDrawResult(memberId, sysId, serialNumber, device);
		return result;
	}
	
	@RequestMapping(value = "/seachWinningRecord", method = RequestMethod.POST)
	@ApiOperation(notes = "查询中奖记录-xujinding" + "</br>返回码" + "</br>0-成功" + "</br>1001-参数异常" + "</br>1004-系统异常", value = "查询中奖记录-xujinding")
	@ResponseBody
	@Valids({ @Valid(name = "startTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2), @Valid(name = "endTime", required = false, regex = RegexUtils.DATE_TIME_FORMAT2),
			@Valid(name = "pageNumber", required = true, minValue = 1), @Valid(name = "pageSize", required = true, minValue = 1), })
	public PageResult<List<WinningRecordVo>> seachWinningRecordPage(@ApiParam(name = "ruleId", value = "抽奖编号", required = false) @RequestParam(required = false) Long ruleId,
			@ApiParam(name = "phone", value = "客户帐号", required = false) @RequestParam(required = false) String phone,
			@ApiParam(name = "startTime", value = "开始时间", required = false) @RequestParam(required = false) String startTime,
			@ApiParam(name = "endTime", value = "结束时间", required = false) @RequestParam(required = false) String endTime,
			@ApiParam(name = "sysId", value = "系统Id", required = true) @RequestParam(required = true) Long sysId,
			@ApiParam(name = "pageNumber", value = "页码", required = true) @RequestParam(required = true) Integer pageNumber,
			@ApiParam(name = "pageSize", value = "页长", required = true) @RequestParam(required = true) Integer pageSize)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		WinningRecord wr = new WinningRecord();
		wr.setWinningRule(ruleId);
		wr.setMobilePhone(phone);
		wr.setStartTime(startTime);
		wr.setEndTime(endTime);
		wr.setSysId(sysId);
		PageResult<List<WinningRecordVo>> pov = winningRecordService.seachWinningRecordPage(wr, pageSize, pageNumber);
		return pov;
		
	}
	
	@Valids({ @Valid(name = "device", required = true, regex = "PC|WAP"), })
	@ResponseBody
	@RequestMapping(value = "/getDrawNumByRuleAndMemberId", method = RequestMethod.POST)
	@ApiOperation(value = "获取某人某规则抽奖次数-xujinding", notes = "获取某人某规则抽奖次数-xujinding")
	@ApiImplicitParams({ @ApiImplicitParam(name = "serialNumber", value = "规则序列号", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "memberId", value = "会员ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "device", value = "平台", required = true, dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "sysId", value = "系统ID", required = true, dataType = "long", paramType = "query") })
	public Result<DrawNum> getDrawNumByRuleAndMemberId(@RequestParam Long memberId, @RequestParam Long sysId, @RequestParam String serialNumber, String device)
			throws ParseException, CustomException {
		Result<DrawNum> res = new Result<>();
		DrawNum dn = drawNumService.findDrawNumBySerNum(memberId, serialNumber, sysId, device);
		res.setErrorCode(ErrorCode.SUCCESS);
		res.setData(dn);
		return res;
	}
	
}
